using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace TrackMe.View.Behaviours
{
    public static class TextBoxBehaviourLikeAutoComplete
    {
        public static bool GetActive(TextBox control)
        {
            return (bool)control.GetValue(ActiveProperty);
        }

        public static void SetActive(TextBox control, bool value)
        {
            control.SetValue(ActiveProperty, value);
        }

        public static readonly DependencyProperty ActiveProperty =
            DependencyProperty.RegisterAttached(
            "Active",
            typeof(bool),
            typeof(TextBoxBehaviourLikeAutoComplete),
            new UIPropertyMetadata(false, OnActiveChanged));

        public static ICommand GetCancelCmd(TextBox control)
        {
            return (ICommand)control.GetValue(CancelCmdProperty);
        }

        public static void SetCancelCmd(TextBox control, ICommand value)
        {
            control.SetValue(CancelCmdProperty, value);
        }

        public static readonly DependencyProperty CancelCmdProperty =
            DependencyProperty.RegisterAttached(
            "CancelCmd",
            typeof(ICommand),
            typeof(TextBoxBehaviourLikeAutoComplete),
            new UIPropertyMetadata(null));

        public static ICommand GetSelectCmd(TextBox control)
        {
            return (ICommand)control.GetValue(SelectCmdProperty);
        }

        public static void SetSelectCmd(TextBox control, ICommand value)
        {
            control.SetValue(SelectCmdProperty, value);
        }

        public static readonly DependencyProperty SelectCmdProperty =
            DependencyProperty.RegisterAttached(
            "SelectCmd",
            typeof(ICommand),
            typeof(TextBoxBehaviourLikeAutoComplete),
            new UIPropertyMetadata(null));

        static void OnActiveChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            var item = depObj as TextBox;
            if (item == null)
                return;

            if (e.NewValue is bool == false)
                return;

            if ((bool)e.NewValue)
                item.PreviewKeyDown += OnPreviewKeyDown;
            else
                item.PreviewKeyDown -= OnPreviewKeyDown;
        }

        private static void OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            var textBox = (TextBox) sender; 
            var fs = FocusManager.GetFocusScope(textBox);
            var o = FocusManager.GetFocusedElement(fs);
            ICommand cmd;
            switch(e.Key)
            {
                case Key.Escape:

                    cmd = GetCancelCmd(textBox);
                    if (cmd != null)
                        cmd.Execute(textBox);
                    textBox.Focus();
                    break;

                case Key.Down:
                case Key.Up:
                case Key.PageDown:
                case Key.PageUp:

                    if (textBox != null && o == textBox)
                    {
                        cmd = GetSelectCmd(textBox);
                        if (cmd != null)
                            cmd.Execute(e.Key);
                        e.Handled = true;
                    }
                    break;
            }
        }
    }
}